<?php
/**
 * 根据商品id获得该商品的所有图片
 * @param $cid
 * @return @multitype
 */
function getAllImgByProId($id){
    $sql="select a.albumPath from imooc_album a where pid={$id}";
    $rows=fetchAll($sql);
    return $rows;
}

/**
 * 添加商品
 * @return string
 */
function addPro(){
    $arr = $_POST;
    $arr['pubTime'] = time();
    $path = "./uploads";
    $uploadFiles = uploadFile($path);#文件上传
    if (is_array($uploadFiles) && $uploadFiles){
        foreach ($uploadFiles as $k =>$v){
            thumb($path."/".$v['name'],"../image_50/".$v['name'],50,50);
            thumb($path."/".$v['name'],"../image_220/".$v['name'],220,200);
            thumb($path."/".$v['name'],"../image_350/".$v['name'],350,350);
            thumb($path."/".$v['name'],"../image_800/".$v['name'],800,800);
        }
    }
    $res = insert("imooc_pro",$arr);
    $pid = getInsertId();
    if ($res&&$pid){
        foreach ($uploadFiles as $uploadFile) {
            $arr1['pid'] = $pid;
            $arr1['albumPath'] = $uploadFile['name'];
            addAlbum($arr1);
        }
        $msg = "<p>添加成功！</p><a href='addPro.php' target='mainFrame'>继续添加</a>|<a href='listPro.php' 
target='mainFrame'>查看商品列表</a>";
    }else{
        foreach ($uploadFiles as $uploadFile){
            if (file_exists("../image_800/".$uploadFile['name'])){
                unlink("../image_800/".$uploadFile['name']);
            }
            if (file_exists("../image_50/".$uploadFile['name'])){
                unlink("../image_50/".$uploadFile['name']);
            }
            if (file_exists("../image_220/".$uploadFile['name'])){
                unlink("../image_220/".$uploadFile['name']);
            }
            if (file_exists("../image_350/".$uploadFile['name'])){
                unlink("../image_350/".$uploadFile['name']);
            }
        }
        $msg = "<p>添加失败！</p><a href='addPro.php' target='mainFrame'>重新添加</a>";
    }
    return $msg;
}

/**
 * 编辑商品
 * @param $id
 * @return string
 */
function editPro($id){
    $arr = $_POST;
    $path = "./uploads";
    $uploadFiles = uploadFile($path);
    if (is_array($uploadFiles)&&$uploadFiles){
        foreach ($uploadFiles as $k => $uploadFile){
            thumb($path."/".$uploadFile['name'],"../image_50/".$uploadFile['name'],50,50);
            thumb($path."/".$uploadFile['name'],"../image_220/".$uploadFile['name'],220,220);
            thumb($path."/".$uploadFile['name'],"../image_350/".$uploadFile['name'],350,350);
            thumb($path."/".$uploadFile['name'],"../image_800/".$uploadFile['name'],800,800);
        }
    }
    $where = "id={$id}";
    $res = update("imooc_pro",$arr,$where);
    $pid = $id;
    if ($res && $pid){
        if ($uploadFiles && is_array($uploadFiles)){
            foreach ($uploadFiles as $uploadFile){
                $arr1['pid'] = $pid;
                $arr1['albumPath'] = $uploadFile['name'];
                addAlbum($arr1);
            }
        }
        $msg = "<p>编辑成功！</p><a href='listPro.php' target='mainFrame'>查看商品</a>";
    }
    else{
        if (is_array($uploadFiles)&&$uploadFiles){
            foreach ($uploadFiles as $uploadFile){
                if (file_exists("../image_800/".$uploadFile['name'])){
                    unlink("../image_800/".$uploadFile['name']);
                }
                if (file_exists("../image_50/".$uploadFile['name'])){
                    unlink("../image_50/".$uploadFile['name']);
                }
                if (file_exists("../image_220/".$uploadFile['name'])){
                    unlink("../image_220/".$uploadFile['name']);
                }
                if (file_exists("../image_350/".$uploadFile['name'])){
                    unlink("../image_350/".$uploadFile['name']);
                }
            }
        }
        $msg = "<p>编辑失败！</p><a href='listPro.php' target='mainFrame'>重新编辑</a>";
    }
    return $msg;
}

/**
 * 删除商品
 * @param $id
 * @return string
 */
function delPro($id){
    $where = "id={$id}";
    $res = delete("imooc_pro",$where);
    $proImages = getAllImgByProId($id);#根据id获得全部商品图片
    if ($proImages && is_array($proImages)){
        foreach ($proImages as $proImage){
            if (file_exists("uploads/".$proImage['albumPath'])){
                unlink("uploads/".$proImage['albumPath']);
            }
            if (file_exists("../image_50/".$proImage['albumPath'])){
                unlink("../image_50/".$proImage['albumPath']);
            }
            if (file_exists("../image_220/".$proImage['albumPath'])){
                unlink("../image_220/".$proImage['albumPath']);
            }
            if (file_exists("../image_350/".$proImage['albumPath'])){
                unlink("../image_350/".$proImage['albumPath']);
            }if (file_exists("../image_800/".$proImage['albumPath'])){
                unlink("../image_800/".$proImage['albumPath']);
            }
        }
    }
    $where1 = "pid={$id}";
    $res1 = delete("imooc_album",$where1);
    if ($res && $res1){
        $msg = "删除成功！<br><a href='listPro.php' target='mainFrame'>查看商品列表</a>";
    }else{
        $msg = "删除失败！<br><a href='listPro.php' target='mainFrame'>重新删除</a>";
    }
    return $msg;
}

/**
 * 得到商品的所有信息
 * @return @multitype
 */
function getAllProByAdmin(){
    $sql="select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,
p.isShow,p.isHot,c.cName from imooc_pro as p join imooc_cate c on p.cId=c.id";
    $rows=fetchAll($sql);
    return $rows;
}

/**
 * 根据id得到商品的详细信息
 * @param $id
 * @return @multitype
 */
function getProInfo($id){
    $sql="select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,
p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.id={$id}";
    $row = fetchOne($sql);
    return $row;
}

/**
 * 获得全部商品id和商品名称
 * @return multitype
 */
function getProAllId(){
    $sql = "select id,pName from imooc_pro ORDER BY id ASC ";
    $rows = fetchAll($sql);
    return $rows;
}

/**
 * 根据分类id获取4条产品信息
 * @param $cid
 * @return multitype
 */
function getProCid($cid){
    $sql="select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,
p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.cId={$cid} limit 4";
    $rows=fetchAll($sql);
    return $rows;
}

/**
 * 得到另外四条数据
 * @param $cid
 * @return multitype
 */
function getProOtherCid($cid){
    $sql="select p.id,p.pName,p.pSn,p.pNum,p.mPrice,p.iPrice,p.pDesc,p.pubTime,p.isShow,p.isHot,c.cName,
p.cId from imooc_pro as p join imooc_cate c on p.cId=c.id where p.cId={$cid} limit 4";
    $rows=fetchAll($sql);
    return $rows;
}